package com.ruoyi.hospital.treatment.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.medicine.domain.TPatientMedicine;
import com.ruoyi.hospital.medicine.service.ITPatientMedicineService;
import com.ruoyi.hospital.registered.domain.TRegistered;
import com.ruoyi.hospital.registered.service.ITRegisteredService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 门诊挂号Controller
 *
 * @author Shawn
 * @date 2024-02-21
 */
@Controller
@RequestMapping("/hospital/treatment")
public class TTreatmentController extends BaseController {
    private String prefix = "hospital/treatment";

    @Autowired
    private ITRegisteredService tRegisteredService;

    @Autowired
    private ITPatientMedicineService itPatientMedicineService;


    @GetMapping()
    public String treatment() {
        return prefix + "/treatment";
    }


    @GetMapping("/detail")
    public String detail(String registeredId, String patientId, ModelMap modelMap) {
        TRegistered registered = tRegisteredService.selectTRegisteredById(Long.parseLong(registeredId));
        List<TPatientMedicine> tPatientMedicineList = itPatientMedicineService.queryByregistered(registeredId);
        modelMap.addAttribute("registered", registered);
        modelMap.addAttribute("tPatientMedicineList", tPatientMedicineList);
        return prefix + "/detail";
    }

    /**
     * 查询门诊挂号列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TRegistered tRegistered) {
        startPage();
        List<TRegistered> list = tRegisteredService.selectTRegisteredList(tRegistered);
        return getDataTable(list);
    }


    @GetMapping("/illness")
    public String illness(String id, ModelMap modelMap) {
        if (StringUtils.isEmpty(id)) {
            throw new ServiceException("门诊ID不能为空");
        }
        TRegistered tRegistered = tRegisteredService.selectTRegisteredById(Long.parseLong(id));
        if (null == tRegistered) {
            throw new ServiceException("门诊数据不存在");
        }
        modelMap.addAttribute("illness", tRegistered.getIllness());
        modelMap.addAttribute("id", id);
        return prefix + "/illness";
    }

    @PostMapping("/illness")
    @ResponseBody
    public AjaxResult illness(TRegistered tRegistered) {
        return toAjax(tRegisteredService.updateIllness(tRegistered));
    }

    @PostMapping("/prescribe")
    @ResponseBody
    public AjaxResult prescribe(TRegistered tRegistered) {
        return toAjax(tRegisteredService.updatePrescribe(tRegistered));
    }


}
